package com.zkzx.company.base.service;

import com.zkzx.common.company.model.CompanyOperationVO;
import com.zkzx.company.base.mapper.CompanyOperationMapper;
import com.zkzx.company.base.mapper.entity.CompanyOperation;
import com.zkzx.company.base.mapper.entity.CompanyOperationExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年04月30日 10时19分
 */
@Service
public class CompanyOperationService {

    @Resource
    private CompanyOperationMapper companyOperationMapper;

    /**
     * 根据企业id查询运营信息
     * @param companyId 企业id
     * @return 运营信息
     */
    public CompanyOperationVO findVOByCompanyId(Integer companyId){
        CompanyOperation companyOperation = findByCompanyId(companyId);
        if(Objects.isNull(companyOperation)){
            return null;
        }
        return CompanyOperationVO.builder()
                .name(companyOperation.getName())
                .mobile(companyOperation.getMobile())
                .regionId(companyOperation.getRegionid())
                .address(companyOperation.getAddress())
                .regionInfo(companyOperation.getRegioninfo())
                .fareScale(companyOperation.getFarescale())
                .province(companyOperation.getProvince())
                .city(companyOperation.getCity())
                .county(companyOperation.getCounty())
                .build();
    }

    /**
     * 根据企业id查询运营信息
     * @param companyId 企业id
     * @return 运营信息
     */
    public CompanyOperation findByCompanyId(Integer companyId){
        CompanyOperationExample operationExample = new CompanyOperationExample();
        operationExample.createCriteria().andCompanyidEqualTo(companyId);
        Optional<CompanyOperation> companyOperationOne = companyOperationMapper.selectByExample(operationExample).stream().findFirst();
        CompanyOperation companyOperation = null;
        if(companyOperationOne.isPresent()){
            companyOperation = companyOperationOne.get();
        }
        return companyOperation;
    }

    /**
     * 更新运营信息
     * @param companyOperation 运营信息
     */
    public void updateOrInsert(CompanyOperation companyOperation){
        if (Objects.isNull(companyOperation)){
            return;
        }
        // 主键为空是新增
        if (Objects.isNull(companyOperation.getId()) && Objects.nonNull(companyOperation.getCompanyid())){
            companyOperationMapper.insert(companyOperation);
        }else {
            // 主键不为空则修改
            companyOperationMapper.updateByPrimaryKey(companyOperation);
        }
    }
}
